var nc:NetConnection;
var pub_ns:NetStream;
var sub_ns:NetStream;
var gain:Number=50;
var rate:Number=8;
var activityLevel:Number;
var micInterval:Number;
var TIMESLOT:Number=100;
var micMode:String; //Conf||Stat
var audio_mc:MovieClip;

function initMicDevice(param:NetConnection, mode:String)
{
	trace("nc.uri: "+param.uri);
	setNetConnection(param);
	micMode=mode;
	if (audio_mc!=null)
	{
		audio_mc.unloadMovie();
		audio_mc.removeMovieClip();
	}

	if (mode=="Conf")
	{
		var path:MovieClip=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "main");
		audio_mc=path.createEmptyMovieClip("audio_mc", path.getNextHighestDepth());
	}
	else if (mode=="Stat")
	{
		var path:MovieClip=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "main");
		audio_mc=path.createEmptyMovieClip("audio_mc", path.getNextHighestDepth());	
	}	
}

function setNetConnection(param:NetConnection)
{
	nc=param;
	if (nc!=null)
	{
		pub_ns=new NetStream(nc);
		sub_ns=new NetStream(nc);	
	}
}

function setGain(param:Number)
{
	gain=param;
}

function setRate(param:Number)
{
	rate=param;
}

function publish(uri:String, type:String)
{
	trace("publish...gain:"+gain+" rate:"+rate+" uri:"+uri);
	var active_mic:Microphone = Microphone.get();
	if (gain!=null) active_mic.setGain(gain);
	if (rate!=null) active_mic.setRate(rate);
	var silenceLevel;
	if (active_mic.gain<=50)
	{
		silenceLevel=10;
	}
	else if (active_mic.gain<=80)
	{
		silenceLevel=20;
	}
	else if (active_mic.gain<=100)
	{
		silenceLevel=30;
	}
	active_mic.setSilenceLevel(silenceLevel, 2000);
	/*var audio_mc:MovieClip;
	audio_mc=_root.createEmptyMovieClip("audio_mc", _root.getNextHighestDepth());
	audio_mc.attachAudio(active_mic);*/
	//trace("itemPortlet:"+itemPortlet+" audio_mc:"+audio_mc+" depth:"+itemPortlet.getNextHighestDepth());
	
	/*var path:MovieClip;
	if (type=="live")
	{
		path=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "main");
	}
	else if (type=="record")
	{
		path=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "main");
	}

	if (path.VideoArea!=null) 
	{
		path.VideoArea.unloadMovie();
		path.VideoArea.removeMovieClip();
	}
	var videoArea:MovieClip=path.attachMovie("videoarea", "VideoArea", path.getNextHighestDepth());
	var videoAudio:Sound=new Sound(videoArea);
	trace("path:"+path+" depth:"+path.getNextHighestDepth());*/

	// audio_mc.attachAudio(active_mic);
	pub_ns.attachAudio(active_mic);
	// pub_ns.publish(false);
	pub_ns.publish(uri, type);
	activity();
}

function subscribe(uri:String)
{
	sub_ns.play(uri);
}

function pause(uri:String)
{
	pub_ns.publish(false);
	if (micIntervalId!=null) 
		clearInterval(micIntervalId);	
}

function resume(uri:String)
{
	// pub_ns.publish(false);
	pub_ns.publish(uri, "append");
	activity();
}

function close()
{
	pub_ns.publish(false);
	pub_ns.close();
	sub_ns.close();
	if (micIntervalId!=null) 
		clearInterval(micIntervalId);
	if (audio_mc!=null)
	{
		audio_mc.unloadMovie();
		audio_mc.removeMovieClip();
	}	
}

function activity()
{
	if (micIntervalId!=null) 
		clearInterval(micIntervalId);
	micIntervalId=setInterval(onActivity, TIMESLOT);

	var active_mic:Microphone=Microphone.get();
	active_mic.onActivity=function(active:Boolean) 
	{
		if (active)
		{
			Packages.components.readingRecorder.RecordingSpace.microphoneStatus("active");
		}
		else 
		{
			Packages.components.readingRecorder.RecordingSpace.microphoneStatus("silence");
		}
	}
}

function onActivity()
{
	var active_mic:Microphone=Microphone.get();
	//trace("mic activity:"+active_mic.activityLevel);
	if (micMode=="Conf")
		Packages.components.readingRecorder.RecordingGuide.ladderChange(active_mic.activityLevel);
	else if (micMode=="Stat")
		Packages.components.readingRecorder.RecordingSpace.ladderChange(active_mic.activityLevel);
	activityLevel=active_mic.activityLevel;
}

function saveMicSettings()
{
	var obj:Object=new Object();
	obj.gain=gain;
	obj.rate=rate;
	Packages.task.Composition.micSettings=obj;
}

function loadMicSettings()
{
	var obj:Object=Packages.task.Composition.micSettings;
	if (obj!=null)
	{
		gain=obj.gain;
		rate=obj.rate;
	}
}